import { getToken, setToken, removeToken } from '@/utils/auth'
import {
  queryList,
  getRoleFromUserRole,
  currentAndSubPermission,
  rolePermission,
  permissionQueryList,
  saveSetGrant,
  setRoleToUserRole,
  getPermissionByUserId,
  setPermissionToUser
} from '@/api/subsys/userPermission'

const userPermission = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },

  actions: {
    // 用户授权-查询所有用户
    UserQueryList2({ commit }, datas) {
      return new Promise((resolve, reject) => {
        queryList(datas)
          .then(response => {
            // const data = response.data
            // setToken(data.token)
            // commit('SET_TOKEN', data.token)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 用户授权-查询角色列表
    GetRoleFromUserRole({ commit }, datas) {
      return new Promise((resolve, reject) => {
        getRoleFromUserRole(datas)
          .then(response => {
            // const data = response.data
            // setToken(data.token)
            // commit('SET_TOKEN', data.token)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 用户授权-查询角色列表
    GetPermissionByUserId({ commit }, datas) {
      return new Promise((resolve, reject) => {
        getPermissionByUserId(datas)
          .then(response => {
            // const data = response.data
            // setToken(data.token)
            // commit('SET_TOKEN', data.token)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 查询全部权限与子权限
    CurrentAndSubPermission({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        currentAndSubPermission(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 保存权限列表
    SetPermissionToUser({ commit }, datas) {
      return new Promise((resolve, reject) => {
        setPermissionToUser(datas)
          .then(response => {
            // const data = response.data
            // setToken(data.token)
            // commit('SET_TOKEN', data.token)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    SetRoleToUserRole({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        setRoleToUserRole(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 查询选中权限与子权限
    RolePermission({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        rolePermission(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    PermissionQueryList({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        permissionQueryList(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    UserPermissionSaveSetGrant({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        saveSetGrant(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default userPermission
